package com.example.searching_and_sorting;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button0 = (Button)findViewById(R.id.button0); // 放入测试用例
        Button button1 = (Button)findViewById(R.id.button1); // 输入查找值
        final Button LinearSearch = (Button)findViewById(R.id.Linearsearch);
        Button BinarySearch = (Button)findViewById(R.id.BinarySearch); // 二分查找
        Button InsertionSearch = (Button)findViewById(R.id.InsertionSearch); // 插值查找
        Button FibonacciSearch = (Button)findViewById(R.id.FibonacciSearch);  //  斐波那契查找
        Button Tree_table_lookup = (Button)findViewById(R.id.Tree_table_lookup); // 树表查找
        Button HashSearching = (Button)findViewById(R.id.HashSearching);//  哈希查找
        Button selectionSort = (Button)findViewById(R.id.selectionSort); // 选择排序
        Button Shell_Sort = (Button)findViewById(R.id.Shell_Sort);// 希尔排序
        Button HeapSort = (Button)findViewById(R.id.HeapSort);// 堆排序
        Button Binarytree_sort = (Button)findViewById(R.id.Binarytree_sort);// 二叉树排序
        Button insertionSort = (Button)findViewById(R.id.insertionSort);// 插入排序
        Button bubbleSort = (Button)findViewById(R.id.bubbleSort); // 冒泡排序
        Button mergeSort = (Button)findViewById(R.id.mergeSort); // 归并排序
        Button quickSort = (Button)findViewById(R.id.quickSort); // 快速排序
        final int[][] data = new int[1][] ; //  保留测试数据
        final int[] target = new int[1];// 查找值
        final EditText input_test_data = (EditText)findViewById(R.id.input_test_data);
        final EditText input_test_target = (EditText)findViewById(R.id.input_test_target);
        final EditText result = (EditText) findViewById(R.id.result);


        button0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] result = input_test_data.getText().toString().split(" ");
                data[0]= new int[result.length];
                for (int i = 0; i<result.length;i++){
                    data[0][i] = Integer.parseInt(result[i]);
                }
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                target[0] = Integer.parseInt(input_test_target.getText().toString());

            }
        });
        LinearSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Searching.linearSearch(data[0],target[0]));
            }
        });
        BinarySearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Searching.BinarySearch_recursion(data[0],0,data[0].length-1,target[0]));
            }
        });
        InsertionSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Searching.InsertionSearch(data[0],0, data[0].length-1,target[0]));
            }
        });
        FibonacciSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Searching.FibonacciSearch.FibonacciSearch(data[0],target[0]));
            }
        });
        Tree_table_lookup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Searching.Tree_table_lookup(data[0],target[0]));
            }
        });
        HashSearching.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int m = (int) ( data[0].length/0.6);
                List list = Searching.getPrimeNumberToN(m);
                int[] HashMap = new int[(int) list.get(list.size()-1)];
                for (int i = 0 ; i<data[0].length;i++){
                    Searching.Hashinsert(HashMap,(int) list.get(list.size()-1),data[0][i]);
                }
                result.setText(Searching.HashSearching(HashMap,(int) list.get(list.size()-1),target[0]));
            }
        });
        selectionSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Sorting.selectionSort(data[0]));
            }
        });
        Shell_Sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Sorting.Shell_Sort(data[0]));
            }
        });
        HeapSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Sorting.HeapSort(data[0]));
            }
        });
        Binarytree_sort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Sorting.Binarytree_sort(data[0]));
            }
        });
        insertionSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Sorting.insertionSort(data[0]));
            }
        });
        bubbleSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Sorting.bubbleSort(data[0]));
            }
        });
        mergeSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Sorting.mergeSort(data[0]));
            }
        });
        quickSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result.setText(Sorting.quickSort(data[0]));
            }
        });
    }

}
